/**
 * 
 */
package greentea.webloginmodule;

import greentea.webloginmodule.servlets.LoginHandler;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * @author Administrator
 * 
 */
public class WebLoginModuleFilter implements Filter {

	/** Logger for this class and subclasses */
	protected final Log logger = LogFactory.getLog(getClass());
	
	
	/**
	 * 
	 */
	public WebLoginModuleFilter() {
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#destroy()
	 */
	@Override
	public void destroy() {

	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#doFilter(javax.servlet.ServletRequest,
	 *      javax.servlet.ServletResponse, javax.servlet.FilterChain)
	 */
	@Override
	public void doFilter(ServletRequest req, ServletResponse res,
			FilterChain filterChain) throws IOException, ServletException {

		if (LoginHandler.isLogonDone((HttpServletRequest) req,
				(HttpServletResponse) res)) {
			logger.info("user logged in");
			filterChain.doFilter(req, res);
		} else {
			logger.info("user not logged in! >>> redirect to login page");
			req.getRequestDispatcher("/secure/LoginHandler").forward(req,
					res);
			//filterChain.doFilter(req, res);
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see javax.servlet.Filter#init(javax.servlet.FilterConfig)
	 */
	@Override
	public void init(FilterConfig filterConfig) throws ServletException {


	}

}
